package com.redis.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.PostConstruct;

/**
 * @Author 咕唧
 * @Date 2022/6/18 19:51
 * @Version 1.0
 * 自定义序列化方式
 */
@Configuration
public class MyConfig {
    private RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @PostConstruct
    public void configRedisTemplate() {
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //如果不设置默认为jdk的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);

        //这是hash的key和value序列化方式
//        redisTemplate.setHashKeySerializer();
//        redisTemplate.setHashValueSerializer();
        //带包名类型的json序列化方式
//        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        //不带包名类型的序列化方式，单纯的转化为json
//        Jackson2JsonRedisSerializer<Object> objectJackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
    }

}
